package org.example.sringbootdemo.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.sringbootdemo.dao.FruitDao;
import org.example.sringbootdemo.dao.FruitMapper;
import org.example.sringbootdemo.entity.Fruit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/users")
@Tag(name = "用户接口")
public class UserController {

    @GetMapping("/login")
    @Operation(summary = "登录")
    public String login() {
        return "Hello!";
    }

    @GetMapping("/logout")
    @Operation(summary = "登出")
    public String logout() {
        return "Goodbye!";
    }

    @GetMapping("/translate")
    @Operation(summary = "翻译")
    public String translate(String text) {
        return text.toUpperCase();
    }

    @Resource
    private FruitDao fruitDao;
    @Resource
    private FruitMapper fruitMapper;

    @GetMapping("/category")
    @Operation(summary = "货架")
    public List<Fruit> category() {
        return fruitMapper.findAll();
    }

    @GetMapping("/add")
    @Operation(summary = "上架")
    public int add(String name,
                   BigDecimal price,
                   String code) {
        return fruitMapper.insert(new Fruit(name, price, code));
    }

    @GetMapping("/del")
    @Operation(summary = "下架")
    public int del(String code) {
        return fruitMapper.deleteByCode(code);
    }

    @GetMapping("/update")
    @Operation(summary = "修改")
    public int update(String name,
                      BigDecimal price,
                      String code) {
        return fruitMapper.update(new Fruit(name, price, code));
    }
}